#include <crow/http_response.h>

#include "common/api/client.h"

#include "common/log/crow_logger.hpp"
#include "common/log/user_log.hpp"

#include "common/more/string.h"
#include "common/more/uuid.h"

#include "common/helpers/container_helper.hpp"
#include "common/helpers/context_helper.h"
#include "common/helpers/crow_helper.h"
#include "common/helpers/query_helper.h"

#include "translations.h"
#include "ui_controller.h"

#include "ui_users_handler.h"

namespace
{

crow::mustache::context menuContext(const UiSessionPtr session)
{
    auto tr = session->translation();
    auto trMainMenu = tr["mainMenu"];
    auto trBreadCrumbs = tr["breadCrumbs"];
    auto trPage = tr["pages"]["users"];
    auto trTabs = trPage["tabs"];

    Common::ContextList mainMenu;
    mainMenu << Common::ContextValue(
        { { "link", "/users/" },
          { "caption", trMainMenu["users"] },
          { "icon", "ri-user-fill" },
          { "isActive", true } }
    );
    mainMenu << Common::ContextValue(
        { { "link", "/roles/" },
          { "caption", trMainMenu["roles"] },
          { "icon", "ri-shield-user-fill" } }
    );
    mainMenu << Common::ContextValue(
        { { "link", "/teams/" },
          { "caption", trMainMenu["teams"] },
          { "icon", "ri-team-fill" } }
    );
    mainMenu << Common::ContextValue(
        { { "link", "/workflows/" },
          { "caption", trMainMenu["workflows"] },
          { "icon", "ri-mind-map" } }
    );
    mainMenu << Common::ContextValue(
        { { "link", "/item_types/" },
          { "caption", trMainMenu["itemTypes"] },
          { "icon", "ri-article-fill" } }
    );

    Common::ContextList breadCrumbs;
    breadCrumbs << Common::ContextValue(
        { { "link", "" },
          { "caption", trBreadCrumbs["users"] },
          { "isLast", true } }
    );

    Common::ContextList tabs;
    tabs << Common::ContextValue(
        { { "link", "/" },
          { "caption", trTabs["common"] },
          { "isActive", true } }
    );
    tabs << Common::ContextValue(
        { { "link", "/reports" },
          { "caption", trTabs["reports"] } }
    );

    return crow::mustache::context({
        { "tr", trPage },
        { "mainMenu", mainMenu.data() },
        { "breadCrumbs", breadCrumbs.data() },
        { "tabs", tabs.data() },
        { "users", Common::contextFromElements(session->cashManager().users.elements()) },
        { "sessionMessages", session->sessionMessages().toContextList().data() },
    });
}

} // namespace

namespace Ui
{

void UsersHandler::setup(crow::App<>& crowApplication, UiController* controller)
{
    CROW_ROUTE(crowApplication, "/users/")
        .methods("GET"_method)(
            [controller](const crow::request& request)
            {
                const Uuids::Uuid sessionUuid = Common::currentSessionUuid(request);
                const auto session = controller->session(sessionUuid);
                if (!session)
                    return Common::redirect("/");

                // TODO : Проверить права.

                SESSION_LOG_INFO << "Просмотр перечня пользователей.";

                auto context = menuContext(session);
                return crow::response(controller->usersPageTemplate().render(context));
            }
        );

    CROW_ROUTE(crowApplication, "/users/data")
        .methods("GET"_method)(
            [controller](const crow::request& request)
            {
                const Uuids::Uuid sessionUuid = Common::currentSessionUuid(request);
                const auto session = controller->session(sessionUuid);
                if (!session)
                    return Common::redirect("/");

                // TODO : Проверить права.

                // FIXME : Здесь небезопасно летят клиенту все hash-ы паролей, исправить!
                crow::mustache::context context(
                    { { "data", Common::contextFromElements(session->cashManager().users.elements()) } }
                );
                return crow::response(context);
            }
        );
}

} // namespace Ui
